package com.zhuozhou.jdyc.main.main.wight;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.zhuozhou.jdyc.R;


/**
 * Created by 方舟 on 2019/8/26.
 */
public class CustomTabTextView extends FrameLayout {
    private Context mContext;
    private float mHeight;
    private TextView tvTabText;
    private boolean selected = false;
    private Class<?> clx;
    private String tag;
    private Fragment mFragment = null;
    private Bundle bundle;

    public CustomTabTextView(Context context) {
        super(context);
    }

    public CustomTabTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
        mHeight = a.getDimension(R.styleable.auto3d_textSize, 12);
        selected = a.getBoolean(R.styleable.auto3d_bg_selected, false);
        a.recycle();
        mContext = context;
        init();
    }

    public CustomTabTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomTabTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.custom_tab_text_view, this, true);
        tvTabText = findViewById(R.id.tv_tab_text);
        changeBackground(selected);
    }

    public void changeBackground(boolean selected) {
        if (selected) {
            tvTabText.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            tvTabText.setBackgroundColor(ContextCompat.getColor(mContext, R.color.themeColor));
        } else {
            tvTabText.setTextColor(ContextCompat.getColor(mContext, R.color.themeColor));
            tvTabText.setBackground(ContextCompat.getDrawable(mContext, R.drawable.border));
        }
    }

    public void init(String strId, Class<?> clx, Bundle bundle, boolean selected) {
        tvTabText.setText(strId);
        this.clx = clx;
        tag = clx.getName();
        this.bundle = bundle;
        changeBackground(selected);
    }

    public void init(String strId, Class<?> clx, boolean selected) {
        tvTabText.setText(strId);
        this.clx = clx;
        tag = clx.getName();
        changeBackground(selected);
    }

    public Bundle getBundle() {
        return bundle == null ? new Bundle() : bundle;
    }

    public String getTag() {
        return tag;
    }

    public Class<?> getClx() {
        return clx;
    }

    public Fragment getFragment() {
        return mFragment;
    }

    public void setFragment(Fragment fragment) {
        this.mFragment = fragment;
    }

}
